package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.ObjectUtil;
import com.shop.common.result.Result;
import com.shop.dto.SelectClassificationDto;
import com.shop.entity.ClassificationDo;
import com.shop.mapper.ClassificationDoMapper;
import com.shop.service.ClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类别 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2021-01-29
 */
@Service
public class ClassificationServiceImpl extends ServiceImpl<ClassificationDoMapper, ClassificationDo> implements ClassificationService {

    @Override
    public List<SelectClassificationDto> selectClassification() throws Exception {

        QueryWrapper<ClassificationDo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_disable", "N");
        List<ClassificationDo> craftSpecialDemandTypeDos = baseMapper.selectList(queryWrapper);

        List<SelectClassificationDto> selectClassificationDtos = craftSpecialDemandTypeDos.stream().map(k -> {
            SelectClassificationDto selectClassificationDto = new SelectClassificationDto();
            ObjectUtil.annotationToObject(k, selectClassificationDto);
            return selectClassificationDto;
        }).collect(Collectors.toList());

        Map<Long, List<ClassificationDo>> longListMap = craftSpecialDemandTypeDos.stream().filter(k ->
            k.getParentId() != 0
        ).collect(Collectors.groupingBy(ClassificationDo::getParentId));

        selectClassificationDtos.forEach(k ->{
            k.setClassificationDos(longListMap.get(k.getPkClassificationId()));
        });

        selectClassificationDtos = selectClassificationDtos.stream().filter(k -> k.getParentId() == 0).collect(Collectors.toList());

        return selectClassificationDtos;
    }
}
